package com.gl.goods.ui.main.mvp.me;

import android.annotation.SuppressLint;

import com.gl.goods.base.BaseBean;
import com.gl.goods.base.BasePresenter;
import com.gl.goods.net.GetResultErrorMessage;
import com.gl.goods.net.RxScheduler;
import com.gl.goods.ui.login.bean.UserInfoBean;

import io.reactivex.functions.Consumer;

/**
 * Time; 2020/3/15
 * Author: hmc
 * Description:
 */
public class UserFragmentPresenter extends BasePresenter<UserFragmentContract.View> implements UserFragmentContract.Presenter{
    private UserFragmentContract.Model model;
    public UserFragmentPresenter() {
        model = new UserFragmentModel();
    }
    /**获取用户个人信息*/
    @SuppressLint("CheckResult")
    @Override
    public void getUserInfo() {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getUserInfo()
                .compose(RxScheduler.Flo_io_main())
                .as(mView.bindAutoDispose())
                .subscribe(new Consumer<UserInfoBean>() {
                    @Override
                    public void accept(UserInfoBean userInfoBean) throws Exception {
                        mView.getUserInfoSuccess(userInfoBean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
//                        mView.showErrorMessage(GetResultErrorMessage.getInstance().GetResultErrorMessage(throwable));
                        mView.hideLoading();
                    }
                });
    }
    /**退出登录*/
    @Override
    public void logout() {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.logout()
                .compose(RxScheduler.Flo_io_main())
                .as(mView.bindAutoDispose())
                .subscribe(new Consumer<BaseBean>() {
                    @Override
                    public void accept(BaseBean baseBean) throws Exception {
                        mView.logout();
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
//                        mView.showErrorMessage(GetResultErrorMessage.getInstance().GetResultErrorMessage(throwable));
                        mView.hideLoading();
                    }
                });
    }
}
